package com.starry.module.system.core.code.controller.admin;

import cn.hutool.http.HttpStatus;
import com.starry.core.common.domain.R;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.core.web.core.controller.BaseController;
import com.starry.module.system.api.code.domain.qo.CodeRuleConfigPageQo;
import com.starry.module.system.api.code.domain.qo.CodeRuleConfigQo;
import com.starry.module.system.api.code.domain.vo.CodeRuleConfigDtlVo;
import com.starry.module.system.api.code.domain.vo.CodeRuleConfigVo;
import com.starry.module.system.core.code.service.CodeRuleConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 编码规则配置
 * @author xiaoke
 */
@Tag(name = "编码规则配置")
@RestController
@RequestMapping("system/codeRuleConfig")
public class CodeRuleConfigController extends BaseController {

    private final CodeRuleConfigService codeRuleConfigService;

    public CodeRuleConfigController(CodeRuleConfigService codeRuleConfigService) {
        this.codeRuleConfigService = codeRuleConfigService;
    }

    /**
     * 分页查询
     *
     * @param pageQo 查询实体
     * @return 所有数据
     */
    @Operation(summary = "编码规则配置-分页查询")
    @GetMapping("codeRuleConfigPage")
    public R<PageVo<CodeRuleConfigVo>> findPage(CodeRuleConfigPageQo pageQo) {
        return success(this.codeRuleConfigService.page(pageQo));
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "编码规则配置-详情")
    @GetMapping("codeRuleConfigDetails/{id}")
    public R<CodeRuleConfigDtlVo> findById(@PathVariable("id") Long id) {
        return success(this.codeRuleConfigService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param codeRuleConfigQo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "编码规则配置-新增")
    @PostMapping("codeRuleConfigCreate")
    public R<Long> insert(@RequestBody @Validated CodeRuleConfigQo codeRuleConfigQo) {
        return success(HttpStatus.HTTP_CREATED, this.codeRuleConfigService.save(codeRuleConfigQo));
    }

    /**
     * 修改数据
     *
     * @param codeRuleConfigQo 实体对象
     * @return 修改结果
     */
    @Operation(summary = "编码规则配置-修改")
    @PostMapping("codeRuleConfigUpdate")
    public R<Void> update(@RequestBody @Validated CodeRuleConfigQo codeRuleConfigQo) {
        this.codeRuleConfigService.updateById(codeRuleConfigQo);
        return success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @Operation(summary = "编码规则配置-删除")
    @PostMapping("codeRuleConfigRemove")
    public R<Void> delete(@RequestParam("idList") Set<Long> idList) {
        this.codeRuleConfigService.removeByIds(idList);
        return success();
    }

    @GetMapping("generatedCode")
    public R<String> generatedCode(@RequestParam("ruleKey") String ruleKey) {
        return success(this.codeRuleConfigService.generatedCode(ruleKey));
    }

}

